home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / mail110 / log.c < prev    next >
C/C++ Source or Header  |  1994-02-14  |  1KB  |  76 lines

  1. /*==============================================
  2. //
  3. // log.c
  4. //
  5. // log outgoing mail
  6. //
  7. //==============================================
  8. */
  9.  
  10. #include <stdio.h>
  11. #include "mailer.h"
  12.  
  13. #define FROM "From:"
  14.  
  15. int append_to_log (char *txt)
  16. {
  17.     FILE *fplog, *fptxt;
  18.     int res = 0;
  19.     char *p, *buf;
  20.  
  21.     fprintf (stderr, "appending %s to %s\n", txt, log);
  22.     if ((buf = (char *)malloc (1024)) == NULL)
  23.     {
  24.         fprintf (stderr, "log: out of memory\n");
  25.         return 0;
  26.     }
  27.     
  28.     if (!*log)
  29.     {
  30.         fprintf (stderr, "log: no log file specified\n");
  31.         return 0;
  32.     }
  33.  
  34.     if (access (log, 0) != 0)
  35.     {
  36.         fplog = fopen (log, "w");
  37.         if (fplog == NULL)
  38.             perror ("log: failed to create log file");
  39.     }
  40.     else
  41.     {
  42.         fplog = fopen (log, "a");
  43.         if (fplog == NULL)
  44.             perror ("log: failed to open log file");
  45.     }
  46.  
  47.     if (fplog != NULL)
  48.     {
  49.         fptxt = fopen (txt, "r");
  50.         if (fptxt == NULL)
  51.             perror ("log: failed to open txt file");
  52.         else
  53.         {
  54.             while (fgets (buf, 1024, fptxt) != NULL)
  55.             {
  56.                 if (!strncmp (buf, FROM, strlen (FROM)))
  57.                 {
  58.                     p=strchr (buf, ':');
  59.                     *p = ' ';
  60.                     fprintf (fplog, "%s", buf);
  61.                     break;
  62.                 }
  63.             }
  64.             rewind (fptxt);
  65.             while (fgets (buf, 1024, fptxt) != NULL)
  66.                 fputs (buf, fplog);
  67.             fprintf (fplog, "\n\n");
  68.             fclose (fplog); 
  69.             fclose (fptxt);
  70.             res = 1;
  71.         }
  72.      }
  73.     free (buf);
  74.     return res;    
  75.